/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http//www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is ART OWL API.
 *
 * The Initial Developer of the Original Code is University of Roma Tor Vergata.
 * Portions created by University of Roma Tor Vergata are Copyright (C) 2009.
 * All Rights Reserved.
 *
 * The ART OWL API were developed by the Artificial Intelligence Research Group
 * (art.uniroma2.it) at the University of Roma Tor Vergata
 * Current information about the ART OWL API can be obtained at 
 * http://art.uniroma2.it/owlart
 *
 */

package it.uniroma2.art.owlart.utilities;

import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.model.ARTNode;
import it.uniroma2.art.owlart.model.ARTResource;
import it.uniroma2.art.owlart.models.OWLModel;
import it.uniroma2.art.owlart.models.impl.LiteralIteratorWrappingNodeIterator;
import it.uniroma2.art.owlart.models.impl.OWLModelImpl;
import it.uniroma2.art.owlart.models.impl.ResourceIteratorWrappingNodeIterator;
import it.uniroma2.art.owlart.models.impl.URIResourceIteratorWrappingNodeIterator;
import it.uniroma2.art.owlart.models.impl.URIResourceIteratorWrappingResourceIterator;
import it.uniroma2.art.owlart.navigation.ARTLiteralIterator;
import it.uniroma2.art.owlart.navigation.ARTNodeIterator;
import it.uniroma2.art.owlart.navigation.ARTResourceIterator;
import it.uniroma2.art.owlart.navigation.ARTURIResourceIterator;
import it.uniroma2.art.owlart.navigation.RDFIterator;
import it.uniroma2.art.owlart.navigation.RDFIteratorImpl;
import it.uniroma2.art.owlart.vocabulary.RDF;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Utility methods for managing {@link RDFIterator}s
 * 
 * @author Armando Stellato
 * 
 */
public class RDFIterators {

	/**
	 * this method obtains a collection of objects extracted from an {@link RDFIterator}. Note that the
	 * RDFIterator is automatically closed at the end of execution of this method
	 * 
	 * @param <T>
	 * @param rdfIt
	 * @return
	 * @throws ModelAccessException
	 */
	public static <T> Collection<T> getCollectionFromIterator(RDFIterator<T> rdfIt)
			throws ModelAccessException {
		ArrayList<T> collection = new ArrayList<T>();
		while (rdfIt.streamOpen()) {
			collection.add(rdfIt.getNext());
		}
		rdfIt.close();
		return collection;
	}

	/**
	 * this method obtains a set of objects (i.e. removes duplicates) extracted from an {@link RDFIterator}.
	 * Note that the RDFIterator is automatically closed at the end of execution of this method
	 * 
	 * @param <T>
	 * @param rdfIt
	 * @return
	 * @throws ModelAccessException
	 */
	public static <T> Set<T> getSetFromIterator(RDFIterator<T> rdfIt) throws ModelAccessException {
		HashSet<T> set = new HashSet<T>();
		while (rdfIt.streamOpen()) {
			set.add(rdfIt.getNext());
		}
		rdfIt.close();
		return set;
	}

	/**
	 * creates an ARTNodeIterator iterating over all the elements pointed by <code>list</code> saving all the
	 * code needed to parse the list RDF structure
	 * 
	 * @param model
	 * @param list
	 * @param inference
	 * @param graphs
	 * @return
	 * @throws ModelAccessException
	 */
	public static ARTNodeIterator createRDFListIterator(OWLModel model, ARTResource list, boolean inference,
			ARTResource... graphs) throws ModelAccessException {
		return new RDFListIterator(model, list, inference, graphs);
	}

	public static ARTLiteralIterator toLiteralIterator(ARTNodeIterator it) throws ModelAccessException {
		return new LiteralIteratorWrappingNodeIterator(it);
	}

	public static ARTResourceIterator toResourceIterator(ARTNodeIterator it) throws ModelAccessException {
		return new ResourceIteratorWrappingNodeIterator(it);
	}

	public static ARTURIResourceIterator toURIResourceIterator(ARTNodeIterator it)
			throws ModelAccessException {
		return new URIResourceIteratorWrappingNodeIterator(it);
	}

	public static ARTURIResourceIterator toURIResourceIterator(ARTResourceIterator it)
			throws ModelAccessException {
		return new URIResourceIteratorWrappingResourceIterator(it);
	}

	/**
	 * This iterator class provides facilities for managing resources of type rdf:List It is already used in
	 * OWL and SKOS methods such as {@link OWLModelImpl#parseDataRange(ARTResource, ARTResource...)}
	 * 
	 * @author Armando Stellato
	 * 
	 */
	public static class RDFListIterator extends RDFIteratorImpl<ARTNode> implements ARTNodeIterator {

		ARTResource currentNode;
		OWLModel model;
		boolean inference;
		ARTResource[] graphs;

		public RDFListIterator(OWLModel model, ARTResource list, boolean inference, ARTResource... graphs)
				throws ModelAccessException {
			this.model = model;
			this.graphs = graphs;
			this.inference = inference;
			currentNode = list;
		}

		public void close() throws ModelAccessException {

		}

		public ARTNode getNext() throws ModelAccessException {
			if (!streamOpen())
				throw new ModelAccessException("cannot access next node of the list since there are no more!");

			// getting value of current node
			ARTNodeIterator it = model
					.listValuesOfSubjPredPair(currentNode, RDF.Res.FIRST, inference, graphs);
			ARTNode currentValue = it.getNext();
			it.close();

			// updating current node
			ARTResourceIterator resIt = model.listValuesOfSubjObjPropertyPair(currentNode, RDF.Res.REST,
					inference, graphs);
			currentNode = resIt.getNext();
			resIt.close();

			return currentValue;
		}

		public boolean streamOpen() throws ModelAccessException {
			if (currentNode.equals(RDF.Res.NIL))
				return false;
			return true;
		}

	}

}
